package com.java.imauoj.sercive;

import com.java.imauoj.mapper.LoginMapper;
import com.java.imauoj.moudel.Login;
import com.java.imauoj.moudel.ResultVo;
import com.java.imauoj.moudel.Role;
import com.java.imauoj.moudel.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginService {

    @Autowired
    private LoginMapper loginMapper;

    public ResultVo<Login> getUser(String username, String password, HttpSession session){
        ResultVo<Login> re = new ResultVo<>();
        Login login = new Login();
        User user = loginMapper.getUser(username,password);
        if(user!=null) {
            Role role = loginMapper.getRole(user.getRole_id());
            login.setLogin(user);
            login.setRole(role);
            session.setAttribute("login",user);
            session.setAttribute("role",role);
            re.setStatusCode(1);
             re.setMessage("登陆成功");
             re.setData(login);
        }else{
            re.setStatusCode(0);
            re.setMessage("用户名或密码错误");
        }
        return re;
    }

}
